tags: Easy、Linked List
Given the head of a sorted linked list, delete all duplicates such that each element appears only once. Return the linked list sorted as well.
struct ListNode* deleteDuplicates(struct ListNode* head) {
struct ListNode *current = head;
while (current != NULL && current->next != NULL) {
if (current->val == current->next->val) {
struct ListNode *temp = current->next;
current->next = current->next->next;
free(temp);
}
else {
current = current->next;
}
}
return head;
}
tags: Easy、Array
We are given a list nums of integers representing a list compressed with run-length encoding.
Consider each adjacent pair of elements [freq, val] = [nums[2i], nums[2i+1]] (with i >= 0). For each such pair, there are freq elements with value val concatenated in a sublist. Concatenate all the sublists from left to right to generate the decompressed list.
Return the decompressed list.
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
int* decompressRLElist(int* nums, int numsSize, int* returnSize) {
int totalSize = 0;
for (int i = 0; i < numsSize; i += 2) {
totalSize += nums[i];
}
int* decompressedList = (int*)malloc(totalSize * sizeof(int));
int index = 0;
for (int i = 0; i < numsSize; i += 2) {
int freq = nums[i];
int val = nums[i+1];
for (int j = 0; j < freq; j++) {
decompressedList[index++] = val;
}
}
*returnSize = totalSize;
return decompressedList;
}